﻿using System.Collections.Generic;

namespace Mozi.Live.SDP
{
    /// <summary>
    /// 媒体描述信息
    /// </summary>
    public class SDPMediaDescription
    {
        /// <summary>
        /// 媒体摘要信息
        /// </summary>
        public SDPMedia Media { get; set; }
        /// <summary>
        /// 媒体标题
        /// </summary>
        public string MediaTitle { get; set; }
        /// <summary>
        /// 媒体链接信息，这个取决于会话级别是否进行了设置
        /// </summary>
        public SDPConnection Connection { get; set; }
        /// <summary>
        /// 频带信息
        /// </summary>
        public List<SDPBandWidth> BandWidths { get; set; }
        /// <summary>
        /// 已过时，不再使用
        /// </summary>
        public string EncryptKeys { get; set; }
        /// <summary>
        /// 附加属性
        /// </summary>
        public List<SDPAttribute> Attributes { get; set; }
        /// <summary>
        /// 转为SDP字符串格式
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            List<string> ls = new List<string>();
            ls.Add($"m={Media}");
            ls.Add($"i={MediaTitle}");
            if (Connection != null)
            {
                ls.Add($"c={Connection}");
            }
            if (BandWidths != null)
            {
                foreach (var BandWidth in BandWidths)
                {
                    ls.Add($"b={BandWidth}");
                }
            }
            if (Attributes != null)
            {
                foreach (var a in Attributes)
                {
                    ls.Add($"a={a}");
                }
            }
            return string.Join("\r\n", ls);
        }
        /// <summary>
        /// 查找同类型的属性
        /// </summary>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public SDPAttribute[] GetAttributes(SDPAttributeType attribute)
        {
            if (Attributes != null)
            {
                return Attributes.FindAll(x => x.Attribute == attribute).ToArray();
            }
            else
            {
                return null;
            }
        }

       
    }

}
